import request from "@/api/request";
import routerHomeConfig from '@/views/router-home-config'
import { routerData } from "@/store/pinia";
const N404 = "views/404";
let routes = []
const handlerRoutes = (data, arr) => {
    data.forEach((item, index) => {
        if (item.children) {
            arr[index] = {
                path: item.path,
                name: item.index,
                children: [],
                root:item.root
            }
            handlerRoutes(item.children, arr[index].children)
        } else {
            arr[index] = ({
                path: item.path,
                name: item.index,
                root:item.root,
                component: () => { return routerHomeConfig[item.componentName] ? import(`@/${routerHomeConfig[item.componentName]}`) : import(`@/${N404}`) }
            })
        }
    });
}
export const getRouter = async (router) => {
  let res = await request.post("/menu");
  handlerRoutes(res.data, routes);
  routes.forEach((item) => {
    router.addRoute("home", item);
  });
  routerData().updateData(routes);
};
